652D - Nested Segments - CodeForces Solution


data structures sortings *1800

Please click on ads to support us..

C++ Code:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<functional>
#include<vector>
#include<queue>
#include<unordered_map>
#include<numeric>
using namespace std;

const int N = 200004;

struct interval {
    int l, r, i;
}e[N];
int x[N << 1];
int f[N << 1];
int ans[N];
int n;

int lb(int x) { return x & -x; }

void add(int x) {
    while(x <= n * 2) {
	f[x] += 1;
	x += lb(x);
    }
}

int query(int x) {
    int res = 0;
    while(x) {
	res += f[x];
	x -= lb(x);
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    memset(f, 0, sizeof(f));
    int l, r, y, i, j;
    cin >> n;
    j = 0;
    for(i = 0; i < n; i++) {
	cin >> e[i].l >> e[i].r;
	x[j++] = e[i].l;
	x[j++] = e[i].r;
	e[i].i = i;
    }
    sort(e, e + n, [&](auto p, auto q) { return p.r == q.r ? p.l > q.l : p.r < q.r; });
    sort(x, x + (n << 1));
    for(i = 0; i < n; i++) {
	y = upper_bound(x, x + n * 2, e[i].l) - x;
	ans[e[i].i] = query(n * 2) - query(y);
	add(y);
    }
    for(i = 0; i < n; i++) cout << ans[i] << '\n';
    return 0;
}


Comments

Submit
0 Comments
More Questions

Divisible
Three primes
Coprimes
Cost of balloons
One String No Trouble
Help Jarvis!
Lift queries
Goki and his breakup
Ali and Helping innocent people
Book of Potion making
Duration
Birthday Party
e-maze-in
Bricks Game
Char Sum
Two Strings
Anagrams
Prime Number
Lexical Sorting Reloaded
1514A - Perfectly Imperfect Array
580A- Kefa and First Steps
1472B- Fair Division
996A - Hit the Lottery
MSNSADM1 Football
MATCHES Playing with Matches
HRDSEQ Hard Sequence
DRCHEF Doctor Chef
559. Maximum Depth of N-ary Tree
821. Shortest Distance to a Character
1441. Build an Array With Stack Operations